package com.yazi.mxz.fieldsale.controller;


import javax.annotation.Resource;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.fieldsale.entity.beans.FieldSale;
import com.yazi.mxz.fieldsale.entity.beans.StoreGoods;
import com.yazi.mxz.fieldsale.entity.beans.StoreMember;
import com.yazi.mxz.fieldsale.entity.beans.Version;
import com.yazi.mxz.fieldsale.repository.FieldSaleRepository;
import com.yazi.mxz.fieldsale.repository.StoreGoodsRepository;
import com.yazi.mxz.fieldsale.repository.StoreMemberRepository;



/**
 * 数据同步
 * @author mo_yq5
 * @date 2015-6-20
 */
@Controller("syncController")
@RequestMapping("/sync")
public class CustomSyncController extends BaseController {
	
	@Resource
	FieldSaleRepository fieldSaleRepository;
	@Resource
	StoreGoodsRepository storeGoodsRepository;
	@Resource
	StoreMemberRepository storeMemberRepository;
	
	/**
	 * 获取版本信息
	 * @return
	 */
	@RequestMapping("/getVersion/json")
	@ResponseBody
	public ObjectResult<?> getJson() {
		ObjectResult<Version> jsonResult = new ObjectResult<Version>();
		
		try {
			jsonResult.setResult(getVersion());
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	
	/**
	 * 获取要提交的售单
	 * @return
	 */
	@RequestMapping("/listSale4Submit/json")
	@ResponseBody
	public ObjectResult<?> list4SubmitJson(Pageable pageable) {
		ObjectResult<Page<FieldSale>> jsonResult = new ObjectResult<Page<FieldSale>>();
		try {
			Page<FieldSale> page = fieldSaleRepository.findByIsFinishedIsTrue(pageable);
			jsonResult.setResult(page);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
	/**
	 * 删除销售单
	 * @param saleId
	 * @return
	 */
	@RequestMapping("/delSaleDo/json")
	@ResponseBody
	public StatusResult delSaleDoJson(long saleId) {
		StatusResult jsonResult = new StatusResult();
		try {
			fieldSaleRepository.delete(saleId);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	/**
	 * 更新商品信息
	 * @param storeGoods
	 * @return
	 */
	@RequestMapping("/updateStoreGoodsDo/json")
	@ResponseBody
	public StatusResult updateStoreGoodsDoJson(StoreGoods storeGoods) {
		StatusResult jsonResult = new StatusResult();
		try {
			if (storeGoodsRepository.exists(storeGoods.getStoreGoodsId())) {
				storeGoodsRepository.delete(storeGoods.getStoreGoodsId());
			}
			storeGoodsRepository.insert(storeGoods);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	/**
	 * 更新店铺管理员(营业员)
	 * @param storeGoods
	 * @return
	 */
	@RequestMapping("/updateStoreMemberDo/json")
	@ResponseBody
	public StatusResult updateStoreMemberDoJson(@RequestBody StoreMember storeMember) {
		StatusResult jsonResult = new StatusResult();
		try {
			storeMember.setMemberId(storeMember.getMember().getMemberId());
			if (storeMemberRepository.exists(storeMember.getMemberId())) {
				storeMemberRepository.delete(storeMember.getMemberId());
			}
			storeMemberRepository.insert(storeMember);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	
	
	/**
	 * 清除所有数据
	 * @param storeGoods
	 * @return
	 */
	@RequestMapping("/clearDataDo/json")
	@ResponseBody
	public StatusResult clearDataJson() {
		StatusResult jsonResult = new StatusResult();
		try {
			fieldSaleRepository.deleteAll();
			storeGoodsRepository.deleteAll();
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	/**
	 * 更新版本信息(部分)
	 * @param version
	 * @return
	 */
	@RequestMapping("/updateVersionDo/json")
	@ResponseBody
	public StatusResult updateVersionDoJson(Version version) {
		StatusResult jsonResult = new StatusResult();
		try {
			Version defVer = getVersion();
			version.setSyncTime(DateUtil.getTimeStamp());
			version.setResetVersion(defVer.getWarVersion());
			versionRepository.deleteAll();
			versionRepository.insert(version);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
}
